package com.yikaiye.android.yikaiye.ui.activity.home;

import android.os.Bundle;

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityProductListBinding;
import com.yikaiye.android.yikaiye.model.TypeProductModel;
import com.yikaiye.android.yikaiye.ui.fragment.ProductListFragment;
import com.yikaiye.android.yikaiye.ui.fragment.viewmodel.ProductListViewModel;
import com.yikaiye.android.yikaiye.utils.UMengUtil;

/**
 * 产品列表
 */
public class ProductListActivity extends BaseActivity<ActivityProductListBinding, ProductListViewModel> {

    private int position;

    @Override
    protected ProductListViewModel bindModel() {
        return getViewModel(this, ProductListViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        UMengUtil.setCountEventId(Constant.PRODUCT_LIST_CLICK);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            position = bundle.getInt(Constant.INDEX_KEY, 0);
            initTitleBar(getString(R.string.product_type));
        }
        viewModel.getTypeList();
    }

    @Override
    protected void initClick() {
        viewModel.typeData.observe(this, typeProductModels -> {
            FragmentPagerItems.Creator creator = FragmentPagerItems.with(context);
            for (TypeProductModel typeModel : typeProductModels) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.NAME_KEY, typeModel.getName());
                creator.add(typeModel.getName(), ProductListFragment.class, bundle);
            }
            FragmentPagerItems items = creator.create();
            FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), items);
            binding.viewPager.setAdapter(adapter);
            binding.tabLayout.setViewPager(binding.viewPager);
            binding.viewPager.setCurrentItem(position);
        });

    }
}